//
//  UIImage+Extension.m
//  MyShoppingApp
//
//  Created by Mac on 16/1/16.
//  Copyright © 2016年 ibokan. All rights reserved.
//

#import "UIImage+Extension.h"

@implementation UIImage (Extension)
//图片的拉伸
-(UIImage *)imageStretch
{
    return [self stretchableImageWithLeftCapWidth:self.size.width*0.5 topCapHeight:self.size.height*0.5];
}

//图片渲染
-(UIImage *)imageRender
{
    //UIImageRenderingModeAlwaysOriginal下会让图片保持原来的颜色
   return [self imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
}

//圆角图片
- (UIImage *)circleImage
{
    //1.开启bitmap上下文,大小为图片大小，NO代表透明（因为图形上下文默认是黑色的方形），0代表不缩放，设置不透明的时候，会有黑色边缘
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    
    // 获得当前bitmap(位图)上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    // 绘图,画一个椭圆（Ellipse:椭圆）
    CGContextAddEllipseInRect(ctx, rect);
    // 裁剪(将会将封闭的区域进行裁剪)
    CGContextClip(ctx);
//    UIRectClip(); 查查这个
    //将图片画到当前的图形上下文中，这个rect可以不和图片一样大，但是图片会填充整个Rect   drawAtpoint  保持图片大小画上去
    [self drawInRect:rect];
    //2.从当前的bitmap图形上下文中获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //3.结束图形上下文
    UIGraphicsEndImageContext();
    return image;
}

//给图片设置圆角
- (UIImage *)setCornerRadius:(CGFloat)radius
{
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    //1.开启一个bitmap图形上下文
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    //2.获取当前图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //3.描述路径，贝塞尔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
    CGContextAddPath(ctx, path.CGPath);
    //裁剪
    CGContextClip(ctx);
    //将图片画到图形上下文中
    [self drawInRect:rect];
    //4.获取图形上下文中裁剪好的图片
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭图形上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

-(UIImage*)imageByScaledToSize:(CGSize)newSize
{
    CGFloat imageSizeMax = MAX(self.size.width, self.size.height);
    CGFloat newSizeMax = MAX(newSize.width, newSize.height);
    UIImage* newImage = nil;
    if (newSizeMax < imageSizeMax)
    {
        UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
        [self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
        newImage = self;
    return newImage;
}


- (UIImage *)scaleImageWithMaxSize:(CGFloat)maxSize {
//    NSData *data = UIImageJPEGRepresentation(self, 1.0);
//    if(data.length < 200 * 1024){//0.25M-0.5M(当图片小于此范围不压缩)
//        return self;
//    }
    
    UIImage *scaledImage = nil;
    CGFloat imageWidth = self.size.width;
    CGFloat imageHeight = self.size.height;
    CGFloat targetWidth = imageWidth;
    CGFloat targetHeight = imageHeight;
    CGFloat imageMaxSize = MAX(imageWidth, imageHeight);
    if (imageMaxSize < maxSize) return self;
    
    CGFloat scale = 0;
    if (imageWidth >= imageHeight) {// 宽比较长
        scale = maxSize / imageWidth;
        targetWidth = maxSize;
        targetHeight = imageHeight * scale;
    } else { // 高比较长
        scale = maxSize / imageHeight;
        targetHeight = maxSize;
        targetWidth = imageWidth * scale;
    }
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(targetWidth, targetHeight), YES, [UIScreen mainScreen].scale);
    CGContextRef cx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(cx, [[UIColor clearColor] CGColor]);
    UIRectFill(CGRectMake(0, 0,targetWidth, targetHeight));
    
    [self drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)];
    scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
    
//    CGImageRef scaledImage = nil;
//    CGFloat imageWidth = self.size.width;
//    CGFloat imageHeight = self.size.height;
//    CGFloat targetWidth = imageWidth;
//    CGFloat targetHeight = imageHeight;
//    CGFloat imageMaxSize = MAX(imageWidth, imageHeight);
//    if (imageMaxSize < maxSize) return self;
//    
//        CGFloat scale = 0;
//        if (imageWidth >= imageHeight) {// 宽比较长
//            scale = maxSize / imageWidth;
//            targetWidth = maxSize;
//            targetHeight = imageHeight * scale;
//        } else { // 高比较长
//            scale = maxSize / imageHeight;
//            targetHeight = maxSize;
//            targetWidth = imageWidth * scale;
//        }
//    
//    scaledImage = CGImageCreateWithImageInRect([self CGImage], CGRectMake(0, 0, targetWidth, targetHeight));
//    return [UIImage imageWithCGImage:scaledImage];
    
}


@end
